package com.xiaoq.hotel.service.impl;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.xiaoq.coms.util.ConstantUtil;
import com.xiaoq.hotel.entity.HotelEntity;
import com.xiaoq.hotel.repository.IHotelRepository;
import com.xiaoq.hotel.service.IHotelService;
import com.xiaoq.sites.vo.SearchResultVo;
import com.xiaoq.vo.PagingVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HotelServiceImpl implements IHotelService {

    private Logger logger = LoggerFactory.getLogger(HotelServiceImpl.class);

    @Autowired
    private IHotelRepository hotelRep;
    @Override
    public void saveHotel(HotelEntity hotel) {
        hotelRep.saveHotel(hotel);
    }

    @Override
    public List<HotelEntity> findHotel(String cityCode) {
        return hotelRep.findHotel(cityCode);
    }

    @Override
    public List<HotelEntity> findHotel(String cityCode, String delTag,PagingVo pvo) {
        return hotelRep.findHotel(cityCode,delTag,pvo);
    }

    @Override
    public List<HotelEntity> findHotel(String cityCode, String recommend) {
        return hotelRep.findHotel(cityCode,recommend);
    }

    @Override
    public HotelEntity findHotelDetail(String id) {
        return hotelRep.findHotelDetail(id);
    }

    @Override
    public void delete(String id) {
         hotelRep.delete(id);
    }

    @Override
    public List<HotelEntity> findShopFoodList(Double longitude, Double latitude, Double localLongitude, Double localLatitude, PagingVo pvo) {
        return hotelRep.findShopFoodList(longitude,latitude,localLongitude,localLatitude,pvo);
    }

    @Override
    public List<SearchResultVo> findHotelFromFuzzyByText(String text,String cityCode, PagingVo pagingVo) {
        List<SearchResultVo> resultVos = new ArrayList<>();
        if (StringUtils.isBlank(text)) {
            return resultVos;
        }

        List<HotelEntity> hotelEntityList = hotelRep.fuzzyByName(text,cityCode,pagingVo);

        if(null == hotelEntityList || hotelEntityList.size()<1){
            return resultVos;
        }

        try {
            for (int i = 0; i < hotelEntityList.size(); i++) {
                SearchResultVo searchResultVo = new SearchResultVo();
                searchResultVo.setId(hotelEntityList.get(i).getId());
                searchResultVo.setHtmlTitle(hotelEntityList.get(i).getName());
                searchResultVo.setSourceTitle(hotelEntityList.get(i).getName());
                searchResultVo.setContent(hotelEntityList.get(i).getExplain());
                searchResultVo.setType(ConstantUtil.SEARCH_RESULT_TYPE_ACCOMMODATION);
                searchResultVo.setZoneCity(hotelEntityList.get(i).getZoneCity());
                searchResultVo.setLongitude(hotelEntityList.get(i).getLongitude());
                searchResultVo.setLatitude(hotelEntityList.get(i).getLatitude());
                resultVos.add(searchResultVo);

            }
            return resultVos;
        }catch(Exception e){
            logger.error(e.getMessage(), e);
        }
        return resultVos;



    }
}
